/** ###################################################################
**     Filename  : Encoder.C
**     Project   : CyberPark
**     Processor : MC9S12XS128MAL
**     Compiler  : CodeWarrior HCS12X C Compiler
**     Date/Time : 2011-8-1, 0:44
**     Contents  :
**         User source code
**
**     (c) Copyright UNIS, a.s. 1997-2008
**     UNIS, a.s.
**     Jundrovska 33
**     624 00 Brno
**     Czech Republic
**     http      : www.processorexpert.com
**     mail      : info@processorexpert.com
** ###################################################################*/

/* MODULE Encoder */

#include "Encoder.h"
#include "Hmi.h"

volatile U16 EncoderValue;
volatile U16 EncoderDuplexValue;
volatile S16 EncoderDeltaValue;
volatile U8 EncoderChoice;


void EncoderInitial(void){
//    HMI_PA_ResetCounter();
    EncoderSetChoice(ENCODER_INT);        
}


void EncoderIrqHandler(void){
    if(EncoderChoice==ENCODER_PA){
        U16 newEncoderValue;
        newEncoderValue=HMI_PA_GetCounterValue();
        EncoderDeltaValue=(S16)(newEncoderValue-EncoderValue);
        EncoderValue=newEncoderValue;
    }else{
        EncoderDeltaValue=(S16)(EncoderDuplexValue-EncoderValue);
        EncoderValue=EncoderDuplexValue;    
    }
    
}

void EncoderDuplexIrqHandler(void){
    if(HMI_ED_GetVal()){
        --EncoderDuplexValue;   
    }else{
        ++EncoderDuplexValue;
    }
    return;
}

void EncoderSetChoice(U8 choice){
    switch(choice){
        case ENCODER_PA:
        {
            HMI_PA_SetCounter(EncoderValue);
            EncoderChoice=ENCODER_PA;
            HMI_PA_DisableEvent();
        }
            break;
        case ENCODER_INT: 
        {
            EncoderDuplexValue=EncoderValue;
            EncoderChoice=ENCODER_INT; 
            HMI_PA_EnableEvent();   
        }
            break;
        default:
            break;
    }
    return;
}



/* END Encoder */
